<?php

class SearchController extends G4T_Controller
{
    const NEWS_PER_PAGE = 10;

    public function newsAction()
    {
        $query = $this->_getParam('q');
        $this->view->query = $query;

        $searcher = new \Managers\Searchers\News();

        $page = (int) $this->_getParam('page');
        $offset = $page > 0 ? ($page - 1) * self::NEWS_PER_PAGE : 0;
        $searcher->setLimit($offset, self::NEWS_PER_PAGE);
        $searcher->addQuery($query);

        $manager = new \Managers\News();
        try {
            $resultManager = new \Managers\Result\Base($searcher, $manager);
            $resultManager->setDecorator(new \Managers\Decorators\NewsSearch($query));
            $result = $resultManager->getResult();
            $this->view->news = $result;
            $total = $resultManager->getTotalResults();
            $this->view->total = $total;
            $this->displayPaginantor($total, $page, self::NEWS_PER_PAGE);
        } catch (LogicException $e) {
            $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
        } catch (Exception $e) {
            $this->handleException($e);
        }
    }
}
